<template>
  <div class="app-container">
    <el-row class="wrapper codeRep">
      <el-col :span="24" class="warp-main">
        <div class="el-bar">
          <!--工具条-->
          <el-col :span="24" class="toolbar" style="padding-top:10px">
            <el-form :inline="true">
              <el-form-item>
                <el-button @click="preAdd" icon="el-icon-plus" type="primary">代码库</el-button>
              </el-form-item>
              <el-form-item>
                <el-input v-model="queryKey" placeholder="请输入搜索关键字">
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-button @click="codeRepQuery" icon="el-icon-search" type="primary">查询</el-button> 共{{total}}条记录
              </el-form-item>
            </el-form>
          </el-col>

          <!--数据列表-->
          <el-table :data="queryData" highlight-current-row style="width: 100%;">
            <el-table-column :show-overflow-tooltip="true" prop="codeName" label="名称" sortable></el-table-column>
            <el-table-column :show-overflow-tooltip="true" prop="codeUrl" label="代码源" sortable></el-table-column>
            <el-table-column :show-overflow-tooltip="true" prop="remark" label="备注" sortable></el-table-column>
            <el-table-column :formatter="formatDate" :show-overflow-tooltip="true" prop="createTime" label="时间" sortable></el-table-column>
            <el-table-column label="操作" fixed="right" width="200">
              <template slot-scope="scope">
                <el-button icon="el-icon-edit" @click="preUpdateCodePwd(scope.$index, scope.row)" type="primary" size="mini">修改</el-button>
                <el-button icon="el-icon-delete" @click="deleteCodePwd(scope.$index, scope.row)" type="danger" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination style="margin-left:10px" background small layout="prev,pager,next" @current-change="handleCurrentPageChange" :total="total" :page-size="rowNum" :current-page="pageNum" :page-count="pages">
          </el-pagination>

          <el-dialog :title="title" width="450px" :visible.sync="visible" :close-on-click-modal="true">
            <el-form :inline="true" ref="f1" :rules="rules1" :model="form1" label-width="80px" class="form-top">
              <el-form-item label="名称" prop="codeName">
                <el-input v-model="form1.codeName" style="width:300px" placeholder="请输入代码库名称"></el-input>
              </el-form-item>
              <el-form-item label="地址" prop="codeUrl">
                <el-input v-model="form1.codeUrl" style="width:300px" placeholder="eg http://192.168.2.23:8080/svn/module"></el-input>
              </el-form-item>
              <el-form-item label="用户名" prop="codeUserName">
                <el-input v-model="form1.codeUserName" style="width:300px" placeholder="请输入svn用户名"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="codePwd">
                <el-input v-model="form1.codePwd" style="width:300px" placeholder="请输入svn密码"></el-input>
              </el-form-item>
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="form1.remark" style="width:300px" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button class="iconfont icon-chehui" @click="visible=false">取消</el-button>
              <el-button class="iconfont icon-fuxuan" @click="saveCodePwd()" type="primary">确定</el-button>
            </div>
          </el-dialog>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { Notification } from 'element-ui';
export default {
  data() {
    return {
      //查询关键词
      queryKey: '',
      //分页信息
      pages: 0,  //一共有多少页
      pageNum: 1,//当前查询的页码
      rowNum: 10,//每页显示的行数
      total: 0,//总数

      //查询结果信息
      queryData: [],

      //弹出框
      title: '新增代码库',
      visible: false,
      form1: {
        id: '',
        codeName: '',
        codeUrl: '',
        codeUserName: '',
        codePwd: '',
        codeType: 1,
        remark: ''
      },
      rules1: {
        codeName: [
          { validator: this.validateCodeName, trigger: 'blur' }
        ],
        codeUrl: [
          { validator: this.validateCodeUrl, trigger: 'blur' }
        ],
        codeUserName: [
          { validator: this.validateCodeUserName, trigger: 'blur' }
        ],
        codePwd: [
          { validator: this.validateCodePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //验证
    validateCodeName: function(rule, value, callback) {
      if (value == '') {
        callback(new Error('请输入代码库名称'));
      } else {
        callback();
      }
    },
    validateCodeUrl: function(rule, value, callback) {
      if (value == '') {
        callback(new Error('请输入地址'));
      } else {
        callback();
      }
    },
    validateCodeUserName: function(rule, value, callback) {
      if (value == '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    },
    validateCodePwd: function(rule, value, callback) {
      if (value == '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    },
    //查询
    codeRepQuery() {
      this.queryData=[];
      let url = this.projectConfig.URL_API_CI_CODE_SOURCE;
      let teamId = this.userBaseInfo.getCurrentSelectedTeam();
      url += "?key=" + this.queryKey
        + "&teamId=" + teamId
        + "&pageNum=" + this.pageNum
        + "&rowNum=" + this.rowNum;
      this.httpClient.httpClientget(url).then((response) => {
        // 遮罩结束
        this.queryData = response.data.list;
        this.pages = response.data.pages;
        this.total = response.data.total;
      }).catch(error => {
        // 遮罩结束
      });
    },
    //新增代码库前处理
    preAdd() {
      if (this.$refs['f1'] != undefined) {
        this.$refs['f1'].resetFields();
      }
      this.form1.id = "";
      this.form1.codeName = "";
      this.form1.codeUrl = "";
      this.form1.codeUserName = "";
      this.form1.codePwd = "";
      this.form1.codeType = 1;
      this.form1.remark = '';
      this.title = '新增代码库';
      this.visible = true;
    },
    //保存
    saveCodePwd() {
      this.$refs['f1'].validate((valid) => {
        if (valid) {
          this.visible = false;
          if (this.form1.id == '') {
            //新增
            this.form1.team = this.userBaseInfo.getCurrentSelectedTeam();
            this.form1.createUser = this.userBaseInfo.getUserId();
            this.form1.updateUser = this.userBaseInfo.getUserId();
            let url = this.projectConfig.URL_API_CI_CODE_SOURCE;
            this.httpClient.httpClientPost(url, this.form1).then((response) => {
              this.codeRepQuery();
            });
          } else {
            //修改
            this.form1.updateUser = this.userBaseInfo.getUserId();
            let url = this.projectConfig.URL_API_CI_CODE_SOURCE;
            this.httpClient.httpClientPatch(url, this.form1).then((response) => {
              this.codeRepQuery();
            });
          }
        }
      });
    },
    // 删除代码库
    deleteCodePwd: function(index, row) {
      this.$confirm("此操作将永久删除此代码库, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let url = this.projectConfig.URL_API_CI_CODE_SOURCE;
        url += "?id=" + row.id;
        this.httpClient.httpClientDelete(url).then((response) => {
          this.codeRepQuery();
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消操作"
        });
      });
    },

    //更新代码库
    preUpdateCodePwd: function(index, row) {
      this.form1.id = row.id;
      this.form1.codeName = row.codeName;
      this.form1.codeUrl = row.codeUrl;
      this.form1.codeUserName = row.codeUserName;
      this.form1.codePwd = row.codePwd;
      this.form1.codeType = row.codeType;
      this.form1.remark = row.remark;
      this.form1.team = row.team;
      this.title = '修改代码库';
      this.visible = true;
    },
    //分页点击事件
    handleCurrentPageChange(val) {
      this.pageNum = val;
      //查询
      this.codeRepQuery();
    },
    formatDate: function(row, column, cellValue) {
      if (cellValue == '' || cellValue == undefined) {
        return '';
      }
      return cellValue.split('.')[0];
    }
  },
  mounted: function() {
    this.codeRepQuery();
  },
  computed: {
    getCurrentNamespace() {
      return this.$store.getters.namespace;
    }
  },
  watch: {
    getCurrentNamespace(val) {
      this.codeRepQuery();
    }
  }
}
</script>

<style>
.codeRep .el-table tr {
  text-align: center;
}

.codeRep .el-time {
  border: 1px solid;
  border-radius: 4px;
}

.codeRep .el-width {
  width: 400px;
}

.codeRep .el-bar {
  padding-bottom: 50px;
}

.codeRep .el-bar .nbd {
  border-bottom: none;
}

.codeRep .el-dialog__header {
  padding: 20px 20px 10px;
  border-bottom: 1px solid #ccc;
}

.codeRep el-icon-search {
  border: 1px solid #eaeaea;
  border-radius: 4px;
}

.codeRep .search .el-input {
  border: 1px solid #eaeaea;
}

.codeRep .el-date-editor--datetimerange {
  border: 1px solid #eaeaea;
  border-radius: 4px;
}

.codeRep .el-table .cell {
  padding: 0;
}
</style>